package com.sun.umbrella.controller.user;

import com.sun.core.web.result.Result;
import com.sun.umbrella.domain.bo.RoleInfoBO;
import com.sun.umbrella.domain.query.UserEtcQuery;
import com.sun.umbrella.domain.vo.UserEtcSaveVO;
import com.sun.umbrella.domain.vo.UserEtcUpdateVO;
import com.sun.umbrella.domain.vo.UserEtcVO;
import com.sun.umbrella.service.bns.user.UserEtcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Jay
 * @version v1.0
 * @description 用户信息控制器
 * @date 2018-04-26 15:18
 */
@Controller
@Slf4j
@RequestMapping("/pc/user")
public class UserEtcController {


    @Autowired
    private UserEtcService userEtcService;


    /**
     * 分页查询用户信息列表
     *
     * @param userEtcQuery
     * @return
     */
    @RequestMapping("/queryUserPageList")
    @ResponseBody
    public Result<Page<UserEtcVO>> queryUserPageList(UserEtcQuery userEtcQuery
            , @PageableDefault(page = 1, size = 20, sort = "id", direction = Sort.Direction.ASC) Pageable pageable) {
        // 查询
        return Result.wrapSuccessfulResult(userEtcService.queryUserPageList(userEtcQuery, pageable));
    }


    /**
     * 新增用户信息
     *
     * @param userEtcSaveVO
     * @return
     */
    @RequestMapping("/saveUser")
    @ResponseBody
    public Result<Boolean> saveUser(UserEtcSaveVO userEtcSaveVO) {
        // 用户信息新增
        return Result.wrapSuccessfulResult(userEtcService.saveUser(userEtcSaveVO));
    }


    /**
     * 更新用户信息
     *
     * @param userEtcUpdateVO
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Result<Boolean> updateUser(UserEtcUpdateVO userEtcUpdateVO) {
        // 用户信息更新
        return Result.wrapSuccessfulResult(userEtcService.updateUser(userEtcUpdateVO));
    }


    /**
     * 禁用、启用用户
     *
     * @param id
     * @param enable 0 禁用 1 启用 {@link com.sun.umbrella.domain.enums.UserEnableEnum}
     * @return
     */
    @RequestMapping("/updateUserEnable")
    @ResponseBody
    public Result<Boolean> updateUserEnable(Integer id, Integer enable) {
        // 用户禁用、启用更新
        return Result.wrapSuccessfulResult(userEtcService.updateUserEnable(id, enable));
    }

    /**
     * 删除用户
     *
     * @param id 记录id
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Result<Boolean> deleteUser(Integer id) {
        // 删除用户
        return Result.wrapSuccessfulResult(userEtcService.deletedUser(id));
    }


    /**
     * 查询所有角色
     *
     * @return
     */
    @RequestMapping("/queryRoleList")
    @ResponseBody
    public Result<List<RoleInfoBO>> queryRoleList() {
        // 查询所有角色
        return Result.wrapSuccessfulResult(userEtcService.queryRoleList());
    }


    /**
     * @param oldPwd
     * @param newPwd
     * @param confirmPwd
     * @return
     */
    @RequestMapping("/changeUserPassword")
    @ResponseBody
    public Result<?> changeUserPassword(String oldPwd, String newPwd, String confirmPwd) {
        return Result.wrapSuccessfulResult(userEtcService.changeUserPassword(oldPwd, newPwd, confirmPwd));
    }


}
